/**
 * Created by admin on 4/26/16.
 */
var Home=(function($){
    var configMap = {

        },
        jqueryMap = {

        },
        stateMap = {
            current_role : null,             //当前角色
            current_object  : null,          //当前对象
            object_dict : {},               //对象列表
            object_content_dict : {}
        },
        init,setJqueryMap,draw_content;

    draw_content = function(context){
        var $content = jqueryMap['$container']
        $content.html(context)
    }

    setJqueryMap = function(){
        var $container = jqueryMap['$container'],
            $nav = jqueryMap['$nav'],
            $role = $nav.find(".dropdown.role"),
            $object = $nav.find('.dropdown.object'),
            $role_title = $role.find('a span.role-title'),
            $object_title = $object.find('a span.object-title')
        jqueryMap['$role'] = $role
        jqueryMap['$object'] = $object

        // 初始化选择角色事件
        $role.find('li a').click(function(event){
            var $this = $(this),
                role_url = $this.attr("href"),
                title = $this.text()

            if (stateMap.current_role === role_url){
                $role_title.trigger("click")

            } else {
                stateMap.current_role = role_url
                $role_title.text('我是['+title+']')
                $role_title.trigger('click')
            }

            $.ajax({
                url : role_url,
                success : function(result){
                    var $menu = $object.find("ul")
                    $menu.html("")
                    for(index in result){
                        $menu.append('<li><a href="'+result[index].url+'">'+result[index].name +'</a>')
                    }

                    $menu.find('li a').click(function(event){
                        var $this = $(this),
                            url = $this.attr("href"),
                            title = $this.text(),
                            options;

                        $object_title.text('管理['+title+']')
                        if(stateMap.current_object!==url){
                            $.ajax({
                                url: url,
                                success:function(result){
                                    var cls;
                                    result = eval(result)
                                    result["$table_container"] = $container
                                    result['$footer_container'] = jqueryMap["$footer"]
                                    result['$actions_container'] = $nav.find(".actions")
                                    cls = result["page_class"]
                                    cls = cls(result)
                                    console.log(result)
                                }
                            })
                        }
                        $object_title.trigger("click")
                        return false
                    })
                    stateMap.object_dict[role_url] = result
                }
            })
            return false
        })
    }

    init = function($nav,$container,$footer){
        jqueryMap['$container'] = $container
        jqueryMap['$nav'] = $nav
        jqueryMap['$footer'] = $footer
        setJqueryMap()
    }

    return {
        init :init
    }
}(jQuery))